//
//  YQShareSheetView.m
//  industry
//
//  Created by 王雅强 on 2021/2/3.
//  Copyright © 2021 Haier. All rights reserved.
//

#import "YQShareSheetView.h"
#import "WXApi.h"
typedef void (^ButtonAction)(void);
@interface YQShareSheetView ()

@property (nonatomic, strong) XLButton *view;
@property (nonatomic, weak) XLView *containerView;

@property (nonatomic, copy) ButtonAction weixinBA;
@property (nonatomic, copy) ButtonAction pyqBA;


@end

@implementation YQShareSheetView

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)addSheetView {
    if (self.view == nil) {
        // 添加菜单视图到window上，盖住其他组件，防止误操作
        self.view = [[XLButton alloc] init];
        [self.view setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        self.view.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.4];
        WEAKSELF
        [self.view setButtonAction:^(XLButton * _Nonnull button) {
            [weakSelf removeMenuView];
        }];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        [window addSubview:self.view];
        
        
        // 容器视图
        XLView *containerView = [[XLView alloc] init];
        containerView.backgroundColor = [UIColor whiteColor];
        containerView.layer.cornerRadius = 10;
        [self.view addSubview:containerView];
        self.containerView = containerView;
        [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_bottom);
            make.right.left.equalTo(self);
            make.height.mas_equalTo(140 + SAFEAREA_BOTTOM_HEIGHT);
        }];
        XLButton * weiXinButton = [[XLButton alloc] init];
        [weiXinButton setBounds:CGRectMake(0, 0, 30, 55)];
        [weiXinButton setImage:XLIMAGE(@"weixin") forState:UIControlStateNormal];
        [weiXinButton setTitle:@"微信" forState:UIControlStateNormal];
        [weiXinButton.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [weiXinButton setTitleColor:[UIColor colorWithHex:0x353535] forState:UIControlStateNormal];
        [weiXinButton verticalImageAndTitle:17];
        [weiXinButton setButtonAction:^(XLButton * _Nonnull button) {
            if (weakSelf.weixinBA) {
                weakSelf.weixinBA();
            }
            [weakSelf removeMenuView];
//            SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
//            req.bText = YES;
//            req.text = @"分享的内容";
//            req.scene = WXSceneSession;
//
//            [WXApi sendReq:req completion:^(BOOL success) {
//
//            }];
        }];
        
        XLButton * pyqButton = [[XLButton alloc] init];
        [pyqButton setBounds:CGRectMake(0, 0, 30, 55)];
        [pyqButton setImage:XLIMAGE(@"pyq") forState:UIControlStateNormal];
        [pyqButton setTitle:@"朋友圈" forState:UIControlStateNormal];
        [pyqButton.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [pyqButton setTitleColor:[UIColor colorWithHex:0x353535] forState:UIControlStateNormal];
        [pyqButton verticalImageAndTitle:17];
        [pyqButton setButtonAction:^(XLButton * _Nonnull button) {
//            SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
            if (weakSelf.pyqBA) {
                weakSelf.pyqBA();
            }
            [weakSelf removeMenuView];
            
//            req.bText = YES;
//            req.text = @"分享的内容";
//            req.scene = WXSceneTimeline;
//
//            [WXApi sendReq:req completion:^(BOOL success) {
//
//            }];
        }];
        
        XLLabel * label = [[XLLabel alloc]init];
        [label setText:@"分享到"];
        [label setFont:[UIFont systemFontOfSize:12]];
        [label setTextColor:[UIColor colorWithHex:0x353535]];
        
        [containerView addSubview:label];
        [containerView addSubview:weiXinButton];
        [containerView addSubview:pyqButton];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(containerView).offset(19);
            make.centerX.equalTo(containerView);
        }];
        
        [weiXinButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(label.mas_bottom).offset(27);
            make.centerX.equalTo(containerView).multipliedBy(0.5);

        }];
        [pyqButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weiXinButton);
            make.centerX.equalTo(containerView).multipliedBy(1.5);
        }];
        
        [self.view layoutIfNeeded];
        [UIView animateWithDuration:0.5 animations:^{
           
            [containerView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.view.mas_bottom).offset(- 140 - SAFEAREA_BOTTOM_HEIGHT);
            }];
            [self.view layoutIfNeeded];
           
        }];

       
    }
}

- (void)setWeiXinAC:(void(^)(void))weixinAC pyqAC:(void(^)(void))pyqAC{
    self.weixinBA = weixinAC;
    self.pyqBA = pyqAC;
}

- (void)removeMenuView {
    // 将菜单视图从window上移除
//    [self.view layoutIfNeeded];
    [UIView animateWithDuration:0.5 animations:^{
        [self.containerView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_bottom);
        }];
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
        self.view = nil;
    }];

}

@end
